package com.site.tool;
import org.apache.commons.lang.StringUtils;
 

import java.io.*;
import java.util.*;

public class ProxyHandler {
	


	    /**
	     * 扩展IP，如：127.0.0.1
	     *
	     * @param list
	     */
	    public static List<String> expand(Collection<Proxy> list) {
	        List<String> result = new ArrayList<String>();
	        for (Proxy proxy : list) {
	            String ip = proxy.getIp();
	            int end = ip.lastIndexOf(".");
	            String expendIp = ip.substring(0, end) + ".";

	            for (int i = 1; i < 256; i++) {
	                result.add(expendIp + i);
	            }
	        }

	        List<String> extendsIpList = new ArrayList<String>();
	        // HTTP
	        for (String proxy : result) {
	            String data = proxy + ":8080@HTTP";
	            extendsIpList.add(data);
	        }

	        // HTTP
	        for (String proxy : result) {
	            String data = proxy + ":80@HTTP";
	            extendsIpList.add(data);
	        }

	        // SOCK
//	        for(String proxy : result){
//	            String data = proxy + ":1080@SOCK4";
//	            extendsIpList.add(data);
//	        }

	        // SOCK
//	        for(String proxy : result){
//	            String data = proxy + ":3128@HTTP";
//	            extendsIpList.add(data);
//	        }

	        return extendsIpList;
	    }

	    /**
	     * @param file
	     * @param list
	     * @param sockSpec  true:如果是sock，则在前面加“S”  ,false，则不加
	     * @param toFullStr true:保存为：129.0.0.1:8080@HTTP   false:保存为：142.0.0.1:8080
	     */
	    private static void save(String file, Collection<Proxy> list, boolean sockSpec, boolean toFullStr) {
	        FileOutputStream fout = null;
	        try {
	            fout = new FileOutputStream(file);
	            for (Proxy proxy : list) {
	                String str = proxy.toString(sockSpec) + "\r\n";
	                if (toFullStr) {
	                    str = proxy.toFullString() + "\r\n";
	                }
	                fout.write(str.getBytes());
	            }
	            byte[] buf = new byte[1024];
	            fout.close();
	        }
	        catch (Exception e) {
	            if (fout != null) {
	                try {
	                    fout.close();
	                }
	                catch (Exception e1) {
	                }
	            }
	        }
	    }

	    /**
	     * @param file
	     * @param list
	     */
	    private static void save(String file, Collection<String> list) {
	        FileOutputStream fout = null;
	        try {
	            fout = new FileOutputStream(file);
	            for (String proxy : list) {
	                fout.write((proxy + "\r\n").getBytes());
	            }

	            fout.close();
	        }
	        catch (Exception e) {
	            if (fout != null) {
	                try {
	                    fout.close();
	                }
	                catch (Exception e1) {
	                }
	            }
	        }
	    }


	    public static Collection<Proxy> parse(String fileName) {
	        Set<Proxy> httpProxyList = new HashSet<Proxy>();
	        Set<Proxy> sockProxyList = new HashSet<Proxy>();

	        Set<String> ips = new HashSet<String>();
	        try {
	            FileInputStream fin = new FileInputStream(fileName);
	            BufferedReader bin = new BufferedReader(new InputStreamReader(fin));
	            String line = null;

	            while ((line = bin.readLine()) != null) {
	                Proxy proxy = getProxy(line);
	                if (proxy != null) {
	                     String ip = proxy.getIp();
	                    if (ips.contains(ip)) {
	                        continue;
	                    }
	                    if (proxy.isHttp()) {
	                        httpProxyList.add(proxy);
	                    } else {
	                        sockProxyList.add(proxy);
	                    }
	                    ips.add(ip);
	                }
	            }

	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        List<Proxy> list = new ArrayList<Proxy>();
	        list.addAll(httpProxyList);
	        list.addAll(sockProxyList);
	        return list;
	    }

	    private static Proxy getProxy(String line) {
	        if (StringUtils.isEmpty(line)) {
	            return null;
	        }
	        int begin = line.indexOf("@");
	         String ipPort = line;
	        if(begin >= 0){
	               ipPort = line.substring(0, begin);
	        }

	        int split = ipPort.indexOf(":");
	        String ip = ipPort.substring(0, split);
	        String portStr = ipPort.substring(split + 1);
	        int port = Integer.parseInt(portStr);

	        Proxy proxy = new Proxy();
	        if (line.indexOf("SOCK") >= 0) {
	            proxy.setProtocolSock();
	        } else {
	            proxy.setProtocolHttp();
	        }
	        proxy.setIp(ip);
	        proxy.setPort(port);
	        return proxy;
	    }

	    static class Proxy {
	        private String ip;
	        private int port;
	        private String protocol;

	        private static final String PROTOCOL_HTTP = "HTTP";
	        private static final String PROTOCOL_SOCK = "SOCK";

	        public Proxy() {

	        }

	        public Proxy(String ip, String protocol, int port) {
	            this.ip = ip;
	            this.port = port;
	            this.protocol = protocol;
	        }

	        public void setProtocolSock() {
	            this.protocol = PROTOCOL_SOCK;
	        }

	        public void setProtocolHttp() {
	            this.protocol = PROTOCOL_HTTP;
	        }

	        public boolean isHttp() {
	            return PROTOCOL_HTTP.equals(protocol);
	        }

	        public boolean isSock() {
	            return PROTOCOL_SOCK.equals(protocol);
	        }

	        public String toFullString() {
	            String result = ip + ":" + port;
	            if (isSock()) {
	                result = result + "@SOCK4";
	            } else {
	                result = result + "@HTTP";
	            }

	            return result;
	        }

	        public String toString(boolean sockSpec) {
	            String result = ip + ":" + port;
	            if (sockSpec && isSock()) {
	                result = "S" + result;
	            }

	            return result;
	        }

	        public String getIp() {
	            return ip;
	        }

	        public void setIp(String ip) {
	            this.ip = ip;
	        }

	        public int getPort() {
	            return port;
	        }

	        public void setPort(int port) {
	            this.port = port;
	        }

	        public String getProtocol() {
	            return protocol;
	        }

	        public void setProtocol(String protocol) {
	            this.protocol = protocol;
	        }
	    }
	    
	    public static void main(String[] args) {
	        String fileName = "e://proxy.txt";
	        String saveFileName = "e://proxy4.txt";
	        String extendFileName = "e://extend_proxy.txt";
	        Collection<Proxy> list = parse(fileName);

	        // ip 扩展
//	        List<String> result = expand(list);
//	        save(extendFileName, result);

//	        for(Proxy proxy : list){
//	            System.out.println(proxy);
//	        }
	        save(saveFileName, list, true, false);

	    }
}
